/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.can.converter;

import edu.upn.sigemfi.can.bc.SucursalDelegate;
import edu.upn.sigemfi.can.beans.AbstractController;
import edu.upn.sigemfi.can.beans.SucursalController;
import edu.upn.sigemfi.seg.be.Sucursal;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 *
 * @author Orlando ( U )
 */
public class SucursalConverter implements Converter{



  public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) throws ConverterException {
        if (string == null) {
            return null;
        }
        try {
            Long codigo = Long.parseLong(string);
            return ((SucursalController) AbstractController.getBean("SucursalController")).buscar(codigo);
        } catch (Exception e) {
            return null;
        }
    }

    public String getAsString(FacesContext arg0, UIComponent arg1, Object object) throws ConverterException {
        if (object == null) {
            return null;
        }
        if (object instanceof Sucursal) {
            return ((Sucursal) object).getIdSucursal().toString();
        } else {
            throw new IllegalArgumentException("Se encontro un tipo de dato incorrecto");
        }
    }
    }





